package com.zues.samples.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zues.samples.dao.BaseHibernateDAO;
import com.zues.samples.dao.SubjectDAO;
import com.zues.samples.dao.UserDAO;
import com.zues.samples.dao.UserSubjectDAO;
import com.zues.samples.model.UserSubject;

@Repository
public class UserSubjectDAOImp extends BaseHibernateDAO<UserSubject> implements UserSubjectDAO {
@Autowired
private UserDAO userDAO;

@Autowired
private SubjectDAO subjectDAO;
	
	@Override
	public void insertSubjectUsers(List<String> insertList, Integer subjectId) {
		if (insertList != null && insertList.size() > 0) {
			for (int i = 0; i < insertList.size(); i++) {
				String userId = insertList.get(i);
				UserSubject entity;
				try {
					entity = new UserSubject(userDAO.getUserById(userId), subjectDAO.getByIdSubject(subjectId));
					this.save(entity);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public UserSubject getUserSubject(String eus,Integer subjectId){
		String hql = "From UserSubject us WHERE us.userId = :userId AND us.subjectId = :subjectId";
		return (UserSubject) this.createQuery(hql).setString("userId", eus).setInteger("subjectId", subjectId).list().get(0);
	}
	
	@Override
	public void deleteSubjectUsers(List<String> delList, Integer subjectId) {
		if (delList != null && delList.size() > 0) {
			for (int i = 0; i < delList.size(); i++) {
				String userId = delList.get(i);
				UserSubject entity = this.getUserSubject(userId, subjectId);
				this.delete(entity);
//				this.createQuery(String.valueOf(sql)).setString("userId", userId)
//			.executeUpdate();
			}
		}
	}
}
